package com.haojin.exer;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author .29.
 * @create 2022-04-10 15:27
 */
public class EmployeeTest {
    @Test
    public void test1(){
        //使用自然排序
        TreeSet set = new TreeSet();

        Employee a1 = new Employee("郭富城", 48, new MyDate(1995, 5, 9));
        Employee a2 = new Employee("刘德华", 45, new MyDate(2000, 5, 4));
        Employee a3 = new Employee("黎明", 45, new MyDate(2000, 9, 9));
        Employee a4 = new Employee("张学友", 53, new MyDate(1990, 3, 8));
        Employee a5 = new Employee("梁朝伟", 39, new MyDate(2004, 2, 9));

        set.add(a1);
        set.add(a2);
        set.add(a3);
        set.add(a4);
        set.add(a5);

        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    @Test
    public void test2(){
        TreeSet set = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Employee && o2 instanceof Employee){
                    Employee e1 = (Employee) o1;
                    Employee e2 = (Employee) o2;

                    MyDate d1 = e1.getBirthday();
                    MyDate d2 = e2.getBirthday();

                    int minusYear = d1.getYear()-d2.getYear();
                    if(minusYear != 0)
                        return minusYear;

                    int minusMonth = d1.getMonth()-d2.getMonth();
                    if(minusMonth != 0)
                        return minusMonth;

                    int minusDay = d1.getDay()-d2.getDay();
                    return minusDay;
                }
                throw new RuntimeException("输入的数据类型不一致");
            }
        });

        Employee a1 = new Employee("郭富城", 48, new MyDate(1995, 5, 9));
        Employee a2 = new Employee("刘德华", 45, new MyDate(2000, 5, 4));
        Employee a3 = new Employee("黎明", 45, new MyDate(2000, 9, 9));
        Employee a4 = new Employee("张学友", 53, new MyDate(1990, 3, 8));
        Employee a5 = new Employee("梁朝伟", 39, new MyDate(2004, 2, 9));

        set.add(a1);
        set.add(a2);
        set.add(a3);
        set.add(a4);
        set.add(a5);

        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
